#include "BlockQueue.hpp"
#include <pthread.h>
#include <unistd.h>
#include <ctime>
#include <functional>
#include "Task.hpp"

using namespace BlockQueueModule;
using namespace TaskModule;

//消费者线程执行
void *Consumer(void *args)
{
    BlockQueue<Task>* bq = static_cast<BlockQueue<Task> *>(args);
    while(true)
    {
        Task t;
        bq->Pop(&t);
        t.Excute();
        printf("消费者处理完一个任务\n");
    }
}

//生产者线程执行
void *Productor(void *args)
{
    BlockQueue<Task> *bq = static_cast<BlockQueue<Task> *>(args);
    while(true)
    {
        int x = rand() % 10 + 1;
        int y = rand() % 20 + 1;
        Task t(x, y);
        sleep(1);
        bq->Equeue(t);
        printf("生产者生产力一个任务\n");
    }
}

int main()
{
    srand(time(nullptr) ^ getpid());
    BlockQueue<Task> *bq = new BlockQueue<Task>(5);

    //单成产 单消费
    pthread_t c1, p1;
    pthread_create(&c1, nullptr, Consumer, bq);
    pthread_create(&p1, nullptr, Productor, bq);
    pthread_join(c1, nullptr);
    pthread_join(p1, nullptr);
    delete bq;
    return 0;
}